package TUILayer;
import java.util.Scanner;
import CtrLayer.*;
import ModelLayer.*;

/**
 * MainUI is the class responsible for handling the TUI of the main UI.
 * 
 * @author Gruppe 3 
 * @version 21-12-2011
 */
public class MainUI
{
    private SalesUI salesUI;
    private ItemUI itemUI;
    private PersonUI personUI;
    /**
     * Constructs an object of the class MainUI and creates and initialize salesUI, itemUI and personUI.
     */

    public MainUI()
    {
        salesUI = new SalesUI();
        itemUI = new ItemUI();
        personUI = new PersonUI();
    }
    
    /**
     * Starts the mainMenu.
     */

    public static void main(String [] args)
    {
    	MainUI mainUI = new MainUI();
    	mainUI.start();
    }
    
    public void start()
    {
        mainMenu();
    }

    /**
     * Prints Main Menu to terminalwindow and accepts input (choices).
     */
    
    private void mainMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = writeMainMenu(); // show menu and return user's choice
            if (choice == 1)
            {
                salesUI.start();
            }

            else if(choice == 2)
            {
                personUI.start();
            }

            else if(choice == 3)
            {
                itemUI.start();
            }

            else if (choice == 4)
            {
                writeEnd();
                exit = true;
            }
        }
    }
    
    /**
     * Writes the main menu.
     */

    private int writeMainMenu()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        int choice = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Hoved Menu ***");
            System.out.println(" (1) Gå til salgsmenuen ");
            System.out.println(" (2) Gå til person menu ");
            System.out.println(" (3) Gå til vare menu ");
            System.out.println(" (4) Luk systemet ");
            System.out.print("\n Tast dit valg: ");
            try
            {
                choice = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                printCatch();
                returnToMenu();
            }
        }//end while
        return choice;
    }

//     /**
//      * Writes a command in the terminal to answer.
//      * 
//      * @return userName, the username of the employee.
//      */
//     
//     private String inputUserName()
//     {
//         Scanner keyboard = new Scanner(System.in);
//         System.out.print("\n Indtast brugernavn: ");
//         String userName = keyboard.nextLine();
//         return userName;
//     }
//     
//     /**
//      * Writes a command in the terminal to answer.
//      * 
//      * @return password, the password of the employee.
//      */
// 
//     private String inputPassword()
//     {
//         Scanner keyboard = new Scanner(System.in);
//         System.out.print("\n Indtast adgangskode: ");
//         String password = keyboard.nextLine();
//         return password;
//     }
    
     /**
     * Writes a command in the terminal to answer.
     * 
     * @return input, the input you wrote in the terminal.
     */

    private String returnToMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n - Tast retur for at returnere til menuen.");
        String input = keyboard.nextLine();
        return input;
    }
    
    /**
     * Prints out acommand that you should type in a number.
     */

    private void printCatch()
    {
        System.out.println("\n Du skal indtaste et tal - prøv igen!");
    }
    
    /**
     * Prints out a command that the system is closing.
     */

    private void writeEnd()
    {
        System.out.println("\n Systemet lukker ned!");
    }

}
